var FacebookUtils = {}

var AjaxLoader = {
    show: function(element){
        $(element).show();
        
    },
    hide: function(element){
        $(element).hide();
    }
}


var UserTopListing = {
    update: function(){
        AjaxLoader.show('loader-content');
        new Ajax.Request(update_user_top_listing_url, {
            asynchronous: true,
            evalScripts: false,
            evalJson: false,
            method: 'get',
            onComplete: function(transport){
                AjaxLoader.hide('loader-content');
                $('best-players').update(transport.responseText);
            }
        });
        
    }
}

var UserGameStats = {
    update: function(){
        AjaxLoader.show('side-bar-loader');
        new Ajax.Request(update_user_game_stats_url, {
            asynchronous: true,
            evalScripts: false,
            evalJson: false,
            method: 'get',
            onComplete: function(transport){
                AjaxLoader.hide('side-bar-loader');
                $('user_games_info').update(transport.responseText);
            }
        });
    }
}

var WordPuzzle = {
    finishGame: function(points, hours, minutes, seconds){
        finish_game_url = finish_game_url + "?points=" + points;
        finish_game_url = finish_game_url + "&hours=" + hours;
        finish_game_url = finish_game_url + "&minutes=" + minutes;
        finish_game_url = finish_game_url + "&seconds=" + seconds;
        AjaxLoader.show('loader-puzzle');
        new Ajax.Request(finish_game_url, {
            asynchronous: true,
            evalScripts: true,
            evalJson: true,
            method: 'post',
            requestHeaders: {
                Accept: 'application/javascript'
            },
            onComplete: function(transport){
                AjaxLoader.hide('loader-puzzle');
                UserGameStats.update();
                UserTopListing.update();
				//window.alert(transport.responseText);
				
            }
        });
        
    },
    
    newGame: function(firstGame){
        AjaxLoader.show('loader-puzzle');
        new Ajax.Request(NEW_GAME_URL, {
            asynchronous: true,
            evalScripts: true,
            evalJson: true,
            method: 'get',
            requestHeaders: {
                Accept: 'application/json'
            },
            onComplete: function(transport){
                AjaxLoader.hide('loader-puzzle');
                $('puzzle').update("");
				
                var json = transport.responseText.evalJSON();
                
                currentEnigma = json.enigma.enigma;
				console.log(currentEnigma.hint)
                if ( currentEnigma.hint ) {
					
					$('puzzle-hint-container').show();
					
					$('puzzle_hint').update(currentEnigma.hint)	
					
				} else {
					$('puzzle-hint-container').hide();
				}
				
				finish_game_url = json.finish_game_url;
                
                puzzleInfo = new PuzzleInformation(currentEnigma.enigma, 60, 'puzzle');
                puzzle = new Puzzle(puzzleInfo, ext_callbacks);
                puzzle.render(firstGame);
            }
        });
    }
};


var ext_callbacks = {
    'play-again': function(){
        WordPuzzle.newGame(false)
    },
    'share-results': function(){
		FB.ui(publish);
    },
    'invite-friends': function(){
        window.location.href = invitation_path;
    },
    'finished': function(points, hours, minutes, seconds){
        WordPuzzle.finishGame(points, hours, minutes, seconds);
    }
};



var FlashBoxController = {
    errorClass: 'error-flash-box',
    noticeClass: 'notice-flash-box',
    run: function(){
        //check the content
        this.showBox(this.errorClass);
        this.showBox(this.noticeClass);
        
        //window.alert($$('div.error-flash-box')[0].innerHTML)
    },
    showBox: function(elementId){
        var el = $$('div.' + elementId)[0];
        
        if (el.innerHTML.strip() != '') {
            el.blindDown({
                duration: 0.8
            });
            
            Element.blindUp.delay(5, $$('div.' + elementId)[0], {
                duration: 0.8
            });
        }
        
        
    }
    
};

document.observe("dom:loaded", function(){

    FlashBoxController.run()
    
    WordPuzzle.newGame(true);
	
    
});


